package org.web.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.web.project.domain.SysUser;
import org.web.project.service.SysUserService;
import org.web.project.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
* @author 21688
* @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
* @createDate 2024-11-05 21:53:10
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Autowired
    private SysUserMapper userMapper;
    /**
     * 根据条件分页查询用户列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<SysUser> selectUserList(SysUser user) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(user)){
            if (ObjectUtils.isNotEmpty(user.getUserId())){
                wrapper.eq("user_id",user.getUserId());
            }
            if (ObjectUtils.isNotEmpty(user.getUserName())){
                wrapper.like("user_name",user.getUserName());
            }if (ObjectUtils.isNotEmpty(user.getStatus())){
                wrapper.eq("status",user.getStatus());
            }if (ObjectUtils.isNotEmpty(user.getPhonenumber())){
                wrapper.like("phonenumber",user.getPhonenumber());
            }
        }
        return userMapper.selectList(wrapper);
    }

    @Override
    public SysUser selectUserById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public boolean checkUserNameUnique(SysUser user) {
        Long userId = ObjectUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",user.getUserName())
                .eq("del_flag","0");
        SysUser info = userMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkPhoneUnique(SysUser user) {
        Long userId = ObjectUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("phonenumber",user.getPhonenumber())
                .eq("del_flag","0");
        SysUser info = userMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkEmailUnique(SysUser user) {
        Long userId = ObjectUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("email",user.getEmail())
                .eq("del_flag","0");
        SysUser info = userMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}




